*-------------------------------------------------------------------------------*
* DIRECTORIES
*-------------------------------------------------------------------------------*
local l_direc_data `1'
local l_direc_code `2' 
local l_direc_figtab `3' 

local direc_data_raw  `l_direc_data'/raw
local direc_data_clean `l_direc_data'/clean



*---------------------------------------------------------------------------------*
* CITE WEIGHTED
*---------------------------------------------------------------------------------*

use "`direc_data_clean'/mjt_regready_agg050.dta", clear
	
keep if !regexm(journal_name, "(Rest of PubMed)")
keep if year >= 1985

foreach l_var of varlist jshr_frcnt* eshr2_frcnt_all {
	gen lin_`l_var' = `l_var'
	gen A_`l_var' = `l_var'>0
	gen lg_`l_var' = log(`l_var')
	ihstrans `l_var'
}
	
eststo clear
foreach l_pre in lin_ A_ lg_ {
	
	preserve
	
		gen X = `l_pre'eshr2_frcnt_all
		gen Y = .
		
		* keep same sample in FE regs
		qui reghdfe `l_pre'jshr_frcnt_all `l_pre'eshr2_frcnt_all, a(fe_jm fe_mt) 
		keep if e(sample)

		foreach l_vers in jshr_frcnt_all jshr_frcntcit2_all {
		
			replace Y = `l_pre'`l_vers'
		
			qui {
				reghdfe Y X , a(fe_jm fe_mt) 
				local l_r2tot = e(r2)
				reghdfe Y X , a(fe_mt) 
					local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
				reghdfe Y X , a(fe_jm) 
					local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
				reghdfe Y , a( fe_jm fe_mt)
					local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
			}
			eststo: reghdfe Y X , a(fe_jt fe_jm fe_mt) cluster(fe_m)
				local estflg_r2tot = e(r2)
				foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
					estadd scalar `l_flg' = ``l_flg''
				}
				if "`l_pre'"=="lg_" {
					estadd scalar estflg_elast = _b[X]
				}
				else {
					tabstat `l_pre'`l_vers' `l_pre'eshr2_frcnt_all if e(sample), save
					estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])	
				}
		}
		
	restore

}

esttab using"`l_direc_figtab'/tab_het_citew.tex", keep(X) label mlabels(none) ///
	b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
	scalars(estflg_elast estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
	sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
	style(tex) substitute(_ \_  $  \\$  %  \% ) ///
	replace		
	
	
*---------------------------------------------------------------------------------*
* ALT RISK SET
*---------------------------------------------------------------------------------*
use "`direc_data_clean'/mjt_regready_agg050.dta", clear
	
keep if !regexm(journal_name, "(Rest of PubMed)")
keep if year >= 1985

foreach l_var of varlist jshr_frcnt_all eshr2_frcnt_all eshr1_frcnt_all {
	gen lin_`l_var' = `l_var'
	gen A_`l_var' = `l_var'>0
	gen lg_`l_var' = log(`l_var')
	ihstrans `l_var'
}
	
egen ev_j = max(jshr_frcnt_all), by(fe_jm)
replace ev_j = ev_j > 0

egen ev_e = max(eshr2_frcnt_all), by(fe_jm)
replace ev_e = ev_e > 0

gen ev_ej = ev_e & ev_j

egen mea_j = mean(jshr_frcnt_all), by(fe_jm)
	su mea_j if mea_j>0,d
	gen abv_no0j10 = mea_j > `r(p10)'
egen mea_e = mean(eshr2_frcnt_all), by(fe_jm)
	su mea_e if mea_e>0,d
	gen abv_no0e10 = mea_e > `r(p10)'
gen ev_ejnottiny = abv_no0j10 & abv_no0e10
	
gen all = 1

tab ev_j
tab ev_e
tab ev_ej
tab ev_ejnottiny

eststo clear
foreach l_pre in lin_ A_ lg_ {
	
	if "`l_pre'"=="lg_" {
		local elast1a estadd scalar estflg_elast = _b[X]
		local elast1b estadd scalar estflg_elast = _b[X]
		local elast2
	}
	else {
		local elast1a tabstat `l_pre'jshr_frcnt_all `l_pre'eshr2_frcnt_all if e(sample), save
		local elast1b tabstat `l_pre'jshr_frcnt_all `l_pre'eshr1_frcnt_all if e(sample), save
		local elast2 estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])	
	}
	
	preserve
	
		gen X = `l_pre'eshr2_frcnt_all
		gen Y = `l_pre'jshr_frcnt_all
		
		* keep same sample in FE regs
		qui reghdfe `l_pre'jshr_frcnt_all `l_pre'eshr2_frcnt_all, a(fe_jm fe_mt) 
		keep if e(sample)

		* PREFERRED
		foreach l_if in all ev_j ev_e ev_ej ev_ejnottiny {
			
			qui {
				reghdfe Y X if `l_if', a(fe_jm fe_mt) 
				local l_r2tot = e(r2)
				reghdfe Y X if `l_if', a(fe_mt) 
					local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
				reghdfe Y X if `l_if', a(fe_jm) 
					local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
				reghdfe Y if `l_if', a( fe_jm fe_mt)
					local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
			}
			eststo: reghdfe Y X if `l_if', a(fe_jt fe_jm fe_mt) cluster(fe_m)
				local estflg_r2tot = e(r2)
				foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
					estadd scalar `l_flg' = ``l_flg''
				}
				`elast1b'
				`elast2'				
		}
		
		if "`l_pre'"=="lin_" {
			local l_ra replace
		}
		else {
			local l_ra append
		}
		esttab using"`l_direc_figtab'/tab_het_riskset.tex", keep(X) label mlabels(none) ///
			b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
			scalars(estflg_elast estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
			sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
			style(tex) substitute(_ \_  $  \\$  %  \% ) ///
		`l_ra'			
		eststo clear

	restore

}

	

*---------------------------------------------------------------------------------*
* MeSH AGE
*---------------------------------------------------------------------------------*
use "`direc_data_clean'/mjt_regready_agg050.dta", clear
	
keep if !regexm(journal_name, "(Rest of PubMed)")
keep if year >= 1985

foreach l_var of varlist jshr_frcnt_* eshr2_frcnt_* {
	gen lin_`l_var' = `l_var'
	gen A_`l_var' = `l_var'>0
	gen lg_`l_var' = log(`l_var')
	ihstrans `l_var'
}

gen X = eshr2_frcnt_art
gen Y = jshr_frcnt_all
gen XintZ = .
gen XintD = .
reghdfe Y X, a(fe_jm fe_mt)
keep if e(sample)

gen yng = .
gen mid = . 
gen old = .

eststo clear
foreach l_stat in min mea max {
	
	* AGE PER STAT
	if "`l_stat'"=="min" {
		replace yng = `l_stat'_mesh_yearspostfirst==0
		replace mid = `l_stat'_mesh_yearspostfirst>=1 & `l_stat'_mesh_yearspostfirst<=12
		replace old = `l_stat'_mesh_yearspostfirst>=13
	}
	if "`l_stat'"=="mea" {
		replace yng = `l_stat'_mesh_yearspostfirst<=17
		replace mid = `l_stat'_mesh_yearspostfirst>=18 & `l_stat'_mesh_yearspostfirst<=37
		replace old = `l_stat'_mesh_yearspostfirst>=38
	}
	if "`l_stat'"=="max" {
		replace yng = `l_stat'_mesh_yearspostfirst<=20
		replace mid = `l_stat'_mesh_yearspostfirst>=21 & `l_stat'_mesh_yearspostfirst<=57
		replace old = `l_stat'_mesh_yearspostfirst==58				
	}
	eststo: reghdfe Y c.X#c.(yng mid old), a(fe_jm fe_mt) vce(cluster fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot  {
			estadd scalar `l_flg' = ``l_flg''
		}		
	
}
esttab using"`l_direc_figtab'/tab_het_meshage.tex", keep(*X*) label mlabels(none) ///
	b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
	scalars(estflg_r2tot) ///
	sfmt(%5.3fc) nonotes ///
	style(tex) substitute(_ \_  $  \\$  %  \% ) ///
	replace	

eststo clear




*---------------------------------------------------------------------------------*
* ART TYPE
*---------------------------------------------------------------------------------*
use "`direc_data_clean'/mjt_regready_agg050.dta", clear
	
keep if !regexm(journal_name, "(Rest of PubMed)")
keep if year >= 1985

foreach l_var of varlist jshr_frcnt_* eshr2_frcnt_* {
	gen lin_`l_var' = `l_var'
	gen A_`l_var' = `l_var'>0
	gen lg_`l_var' = log(`l_var')
	ihstrans `l_var'
}

gen X1 = eshr2_frcnt_art
gen X2 = eshr2_frcnt_oth
gen Y = .

eststo clear

*ALL Y
replace Y = jshr_frcnt_all
qui {
	reghdfe Y X1 X2, a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
	reghdfe Y X1 X2 , a(fe_mt) 
		local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	reghdfe Y X1 X2 , a(fe_jm) 
		local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	reghdfe Y X2 , a( fe_jm fe_mt)
		local estflg_par2_X1 = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	reghdfe Y X1 , a( fe_jm fe_mt)
		local estflg_par2_X2 = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
}	
eststo: reghdfe Y X1 X2, a(fe_jm fe_mt) vce(cluster fe_m)
	local estflg_r2tot = e(r2)
	foreach l_flg in estflg_r2tot estflg_par2_X1 estflg_par2_X2 estflg_par2_mt estflg_par2_jm {
		estadd scalar `l_flg' = ``l_flg''
	}
	tabstat Y X1 if e(sample), save
	estadd scalar estflg_elast_X1 = (_b[X1]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])	
	tabstat Y X2 if e(sample), save
	estadd scalar estflg_elast_X2 = (_b[X2]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])	
	
*ART Y
replace Y = jshr_frcnt_art
qui {
	reghdfe Y X1 X2, a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
	reghdfe Y X1 X2 , a(fe_mt) 
		local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	reghdfe Y X1 X2 , a(fe_jm) 
		local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	reghdfe Y X2 , a( fe_jm fe_mt)
		local estflg_par2_X1 = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	reghdfe Y X1 , a( fe_jm fe_mt)
		local estflg_par2_X2 = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
}	
eststo: reghdfe Y X1 X2, a(fe_jm fe_mt) vce(cluster fe_m)
	local estflg_r2tot = e(r2)
	foreach l_flg in estflg_r2tot estflg_par2_X1 estflg_par2_X2 estflg_par2_mt estflg_par2_jm {
		estadd scalar `l_flg' = ``l_flg''
	}
	tabstat Y X1 if e(sample), save
	estadd scalar estflg_elast_X1 = (_b[X1]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])	
	tabstat Y X2 if e(sample), save
	estadd scalar estflg_elast_X2 = (_b[X2]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
	
*OTH Y
replace Y = jshr_frcnt_oth
qui {
	reghdfe Y X1 X2, a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
	reghdfe Y X1 X2 , a(fe_mt) 
		local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	reghdfe Y X1 X2 , a(fe_jm) 
		local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	reghdfe Y X2 , a( fe_jm fe_mt)
		local estflg_par2_X1 = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	reghdfe Y X1 , a( fe_jm fe_mt)
		local estflg_par2_X2 = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
}	
eststo: reghdfe Y X1 X2, a(fe_jm fe_mt) vce(cluster fe_m)
	local estflg_r2tot = e(r2)
	foreach l_flg in estflg_r2tot estflg_par2_X1 estflg_par2_X2 estflg_par2_mt estflg_par2_jm {
		estadd scalar `l_flg' = ``l_flg''
	}
	tabstat Y X1 if e(sample), save
	estadd scalar estflg_elast_X1 = (_b[X1]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])	
	tabstat Y X2 if e(sample), save
	estadd scalar estflg_elast_X2 = (_b[X2]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
	
esttab using"`l_direc_figtab'/tab_het_arttype.tex", keep(X1 X2) label mlabels(none) ///
	b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
	scalars(estflg_elast_X1 estflg_elast_X2 estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X1 estflg_par2_X2) ///
	sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
	style(tex) substitute(_ \_  $  \\$  %  \% ) ///
	replace	

eststo clear




*---------------------------------------------------------------------------------*
* EDITOR SPLIT
*---------------------------------------------------------------------------------*
use "`direc_data_clean'/mjt_regready_agg050.dta", clear
	
keep if !regexm(journal_name, "(Rest of PubMed)")
keep if year >= 1985

foreach l_var of varlist jshr_frcnt_all eshr2_frcnt_all eshr1_frcnt_all {
	gen lin_`l_var' = `l_var'
	gen A_`l_var' = `l_var'>0
	gen lg_`l_var' = log(`l_var')
	ihstrans `l_var'
}
	
eststo clear
foreach l_pre in lin_ A_ lg_ {
	
	if "`l_pre'"=="lg_" {
		local elast1a estadd scalar estflg_elast = _b[X]
		local elast1b estadd scalar estflg_elast = _b[X]
		local elast2
	}
	else {
		local elast1a tabstat `l_pre'jshr_frcnt_all `l_pre'eshr2_frcnt_all if e(sample), save
		local elast1b tabstat `l_pre'jshr_frcnt_all `l_pre'eshr1_frcnt_all if e(sample), save
		local elast2 estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])	
	}
	
	preserve
	
		gen X = .
		gen Y = `l_pre'jshr_frcnt_all
		
		* keep same sample in FE regs
		qui reghdfe `l_pre'jshr_frcnt_all `l_pre'eshr2_frcnt_all, a(fe_jm fe_mt) 
		keep if e(sample)

		* ED2
		replace X = `l_pre'eshr2_frcnt_all
		qui {
			reghdfe Y X , a(fe_jm fe_mt) 
			local l_r2tot = e(r2)
			reghdfe Y X , a(fe_mt) 
				local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
			reghdfe Y X , a(fe_jm) 
				local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
			reghdfe Y , a( fe_jm fe_mt)
				local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		}
		eststo: reghdfe Y X , a(fe_jt fe_jm fe_mt) cluster(fe_m)
			local estflg_r2tot = e(r2)
			foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
				estadd scalar `l_flg' = ``l_flg''
			}
			`elast1a'
			`elast2'

		* ED1
		replace X = `l_pre'eshr1_frcnt_all
		qui {
			reghdfe Y X , a(fe_jm fe_mt) 
			local l_r2tot = e(r2)
			reghdfe Y X , a(fe_mt) 
				local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
			reghdfe Y X , a(fe_jm) 
				local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
			reghdfe Y , a( fe_jm fe_mt)
				local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		}
		eststo: reghdfe Y X , a(fe_jt fe_jm fe_mt) cluster(fe_m)
			local estflg_r2tot = e(r2)
			foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
				estadd scalar `l_flg' = ``l_flg''
			}
			`elast1b'
			`elast2'
	restore

}

esttab using"`l_direc_figtab'/tab_het_edsplit.tex", keep(X) label mlabels(none) ///
	b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
	scalars(estflg_elast estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
	sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
	style(tex) substitute(_ \_  $  \\$  %  \% ) ///
	replace		
		
		
		
		


*---------------------------------------------------------------------------------*
* OVER TIME
*---------------------------------------------------------------------------------*
use "`direc_data_clean'/mjt_regready_agg050.dta", clear
	
keep if !regexm(journal_name, "(Rest of PubMed)")

reghdfe jshr_frcnt_all i.year#c.eshr2_frcnt_all, a(fe_jm fe_mt) vce(cluster fe_m)
	preserve
		regsave , ci
		gen bal = 0
		tempfile T0
		save `T0', replace
	restore
	
keep if year >= 1985
reghdfe jshr_frcnt_all i.year#c.eshr2_frcnt_all, a(fe_jm fe_mt) vce(cluster fe_m)
	preserve
		regsave , ci
		gen bal = 1
		tempfile T1
		save `T1', replace
	restore
	
egen minj_year = min(year), by(fe_j)
su minj_year
reghdfe jshr_frcnt_all i.year#c.eshr2_frcnt_all if year >= `r(max)', a(fe_jm fe_mt) vce(cluster fe_m)

regsave , ci
gen bal = 2

append using `T0'
append using `T1'

drop if var == "_cons"
gen year = substr(var,1,4)
destring year , replace force

tw	(scatter coef year if bal==0, msize(medlarge) m(Oh) mc(maroon)) ///
	(rcap ci_* year if bal==0, lc(maroon%50)) ///
	(scatter coef year if bal==1, msize(medlarge) m(S) mc(navy)) ///
	(rcap ci_* year if bal==1, lc(navy%50)) ///
	(scatter coef year if bal==2, msize(medlarge) m(Dh) mc(forest_green)) ///
	(rcap ci_* year if bal==2, lc(forest_green%50)) ///
	, ///
	legend(order( 1 "Unbalanced, full sample" 3 "Unbalanced, 1985-onward" 5 "Balanced") row(1) region(lc(white)) size(small)) ///
	yscale(r( )) ylab(0 .2 .4 .6, angle(horiz) format(%3.1fc) grid gmax gmin) ///
	ytitle("Estimate", orient(hoirz)) yline(0, lc(black)) ///
	xtitle("", margin(small)) xscale(r( )) xlab(, format(%4.0fc) grid gmax gmin)  ///
	graphregion(color(white) lc(white)) bgcolor(white) ///
	xsize(10) ysize(6) scale(1.25) name(byyear, replace)
	
gr export "`l_direc_figtab'/fig_homophily_byyear.pdf", as(pdf) replace		




*---------------------------------------------------------------------------------*
* DATA PROXIES
*---------------------------------------------------------------------------------*
use "`direc_data_clean'/mjt_regready_agg050.dta", clear

preserve
	keep if journal_name == "Rest of PubMed"
	keep fe_m fe_t jshr_frcnt_all
	rename jshr_frcnt_all ojshr_frcnt_all
	gduplicates drop
	tempfile T
	save `T', replace
restore
joinby fe_m fe_t using `T', unmatched(master)
drop _m
recode ojshr_frcnt_all .=0 
	
keep if !regexm(journal_name, "(Rest of PubMed)")
keep if year >= 1985

foreach l_var of varlist jshr_frcnt_all eshr2_frcnt_all ojshr_frcnt_all {
	gen lin_`l_var' = `l_var'
	gen A_`l_var' = `l_var'>0
	gen lg_`l_var' = log(`l_var')
	ihstrans `l_var'
}
	
eststo clear
foreach l_pre in lin_ A_ lg_ {
	
	if "`l_pre'"=="lg_" {
		local elast1 estadd scalar estflg_elast = _b[X]
		local elast2
	}
	else {
		local elast1 tabstat `l_pre'jshr_frcnt_all `l_pre'eshr2_frcnt_all if e(sample), save
		local elast2 estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])		
	}
	
	preserve
		
		* keep same sample in FE regs
		qui reghdfe `l_pre'jshr_frcnt_all `l_pre'eshr2_frcnt_all, a(fe_jm fe_mt) 
		keep if e(sample)
		
		* xtset to make jm proxy
		xtset fe_jm fe_t
		
		* jackknife to make mt proxy
		egen summt_`l_pre'jshr_frcnt_all = sum(`l_pre'jshr_frcnt_all), by(fe_mt)
		gen jaknj_summt_`l_pre'jshr_frcnt_all = summt_`l_pre'jshr_frcnt_all - `l_pre'jshr_frcnt_all
		gen avgjaknj_`l_pre'jshr_frcnt_all = jaknj_summt_`l_pre'jshr_frcnt_all / 16
		
		gen X = `l_pre'eshr2_frcnt_all
		
		* PROXY JM
		qui {
			reghdfe `l_pre'jshr_frcnt_all `l_pre'eshr2_frcnt_all L.`l_pre'jshr_frcnt_all , a(fe_mt)
				local l_r2tot = e(r2)
			reghdfe `l_pre'jshr_frcnt_all `l_pre'eshr2_frcnt_all , a(fe_mt)
				local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
			reghdfe `l_pre'jshr_frcnt_all `l_pre'eshr2_frcnt_all L.`l_pre'jshr_frcnt_all , noabsorb
				local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
			reghdfe `l_pre'jshr_frcnt_all L.`l_pre'jshr_frcnt_all , a(fe_mt)
				local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		}
		eststo: reghdfe `l_pre'jshr_frcnt_all X L.`l_pre'jshr_frcnt_all , a(fe_mt) vce(cluster fe_m)
			local estflg_r2tot = e(r2)
			foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
				estadd scalar `l_flg' = ``l_flg''
			}
			`elast1'
			`elast2'		
		
		* PROXY JM MT
		qui {
			reg `l_pre'jshr_frcnt_all `l_pre'eshr2_frcnt_all L.`l_pre'jshr_frcnt_all avgjaknj_`l_pre'jshr_frcnt_all
				local l_r2tot = e(r2)
			reg `l_pre'jshr_frcnt_all `l_pre'eshr2_frcnt_all avgjaknj_`l_pre'jshr_frcnt_all
				local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
			reg `l_pre'jshr_frcnt_all `l_pre'eshr2_frcnt_all L.`l_pre'jshr_frcnt_all 
				local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
			reg `l_pre'jshr_frcnt_all L.`l_pre'jshr_frcnt_all avgjaknj_`l_pre'jshr_frcnt_all
				local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		}
		eststo: reg `l_pre'jshr_frcnt_all X L.`l_pre'jshr_frcnt_all avgjaknj_`l_pre'jshr_frcnt_all, vce(cluster fe_m)
			local estflg_r2tot = e(r2)
			foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
				estadd scalar `l_flg' = ``l_flg''
			}
			`elast1'
			`elast2'

	restore

}

esttab using"`l_direc_figtab'/tab_het_fedataproxy.tex", keep(X) label mlabels(none) ///
	b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
	scalars(estflg_elast estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
	sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
	style(tex) substitute(_ \_  $  \\$  %  \% ) ///
	replace		
		
		
*---------------------------------------------------------------------------------*
* ALT AGG
*---------------------------------------------------------------------------------*
eststo clear
foreach l_agg in 010 025 050 075 100 {
	
	use "`direc_data_clean'/mjt_regready_agg`l_agg'.dta", clear
	
	keep if !regexm(journal_name, "(Rest of PubMed)")
	keep if year >= 1985

	foreach l_var of varlist jshr_frcnt_all eshr2_frcnt_all {
		gen A_`l_var' = `l_var'>0
		gen lg_`l_var' = log(`l_var')
		ihstrans `l_var'
	}

	gen X = .
	gen Y = .

	replace X = eshr2_frcnt_all
	replace Y = jshr_frcnt_all
	
	reghdfe Y X , a(fe_jm fe_mt) 
	
	keep if e(sample)

	* JM MT
	qui {
		reghdfe Y X , a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(fe_mt) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(fe_jm) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( fe_jm fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jt fe_jm fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		*tabstat jshr_frcnt_all eshr2_frcnt_all if e(sample), save
		*estadd scalar estflg_elast = (_b[X]*(sqrt((r(StatTotal)[1,1])^2+1)/r(StatTotal)[1,1])*(r(StatTotal)[1,2]/sqrt((r(StatTotal)[1,2])^2+1)))
		
}
esttab using"`l_direc_figtab'/tab_het_agg.tex", keep(X) label mlabels(none) ///
	b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
	scalars(estflg_elast estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
	sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
	style(tex) substitute(_ \_  $  \\$  %  \% ) ///
	replace
	
eststo clear

foreach l_agg in 010 025 050 075 100 {
	
	use "`direc_data_clean'/mjt_regready_agg`l_agg'.dta", clear
	
	keep if !regexm(journal_name, "(Rest of PubMed)")
	keep if year >= 1985

	foreach l_var of varlist jshr_frcnt_all eshr2_frcnt_all {
		gen A_`l_var' = `l_var'>0
		gen lg_`l_var' = log(`l_var')
		ihstrans `l_var'
	}

	gen X = .
	gen Y = .

	replace X = A_eshr2_frcnt_all
	replace Y = A_jshr_frcnt_all
	
	reghdfe Y X , a(fe_jm fe_mt) 
	
	keep if e(sample)
	
	* JM MT
	qui {
		reghdfe Y X , a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(fe_mt) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(fe_jm) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( fe_jm fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jt fe_jm fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		tabstat A_jshr_frcnt_all A_eshr2_frcnt_all if e(sample), save
		estadd scalar estflg_elast = (_b[X]/r(StatTotal)[1,1])/(1/r(StatTotal)[1,2])
		
}
esttab using"`l_direc_figtab'/tab_het_agg.tex", keep(X) label mlabels(none) ///
	b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
	scalars(estflg_elast estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
	sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
	style(tex) substitute(_ \_  $  \\$  %  \% ) ///
	append

eststo clear

foreach l_agg in 010 025 050 075 100 {
	
	use "`direc_data_clean'/mjt_regready_agg`l_agg'.dta", clear
	
	keep if !regexm(journal_name, "(Rest of PubMed)")
	keep if year >= 1985

	foreach l_var of varlist jshr_frcnt_all eshr2_frcnt_all {
		gen A_`l_var' = `l_var'>0
		gen lg_`l_var' = log(`l_var')
		ihstrans `l_var'
	}

	gen X = .
	gen Y = .

	replace X = lg_eshr2_frcnt_all
	replace Y = lg_jshr_frcnt_all
	
	reghdfe Y X , a(fe_jm fe_mt) 
	
	keep if e(sample)
	
	* JM MT
	qui {
		reghdfe Y X , a(fe_jm fe_mt) 
		local l_r2tot = e(r2)
		reghdfe Y X , a(fe_mt) 
			local estflg_par2_jm = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y X , a(fe_jm) 
			local estflg_par2_mt = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
		reghdfe Y , a( fe_jm fe_mt)
			local estflg_par2_X = (`l_r2tot'-`e(r2)')/(1-`e(r2)')
	}
	eststo: reghdfe Y X , a(fe_jt fe_jm fe_mt) cluster(fe_m)
		local estflg_r2tot = e(r2)
		foreach l_flg in estflg_r2tot estflg_par2_X estflg_par2_mt estflg_par2_jm {
			estadd scalar `l_flg' = ``l_flg''
		}
		estadd scalar estflg_elast = _b[X]

}
esttab using"`l_direc_figtab'/tab_het_agg.tex", keep(X) label mlabels(none) ///
	b(%5.3fc) se(%5.3fc) star(* 0.1 ** 0.05 *** 0.01) ///
	scalars(estflg_elast estflg_r2tot estflg_par2_jm estflg_par2_mt estflg_par2_X ) ///
	sfmt(%5.3fc %5.3fc %5.3fc %5.3fc %5.3fc) nonotes ///
	style(tex) substitute(_ \_  $  \\$  %  \% ) ///
	append

		
		
		
		
		
		
		
